using System;
using UnityEngine;

namespace Function.SkillEditor.Runtime
{
    public enum EffectPointType
    {
        Avatar = 1, //骨骼 不支持动态创建 自己提供方法查找
        Scene = 2, //场景不动 挂点的位置是初始位置
    }

    [Serializable]
    public class SkillEffectClip : SkillClip
    {
        public Vector3 _pos;
        public Vector3 _rotate;
        public Vector3 _scale;
        public string _bindName;
        public EffectPointType _bindType = EffectPointType.Scene;

        [NonSerialized] private GameObject _effectGo;

        public override void OnExit()
        {
            SkillRunRender.Instance.SendMessage(_entityData._entityId, RenderState.Exit, this);
        }

        public override void OnEnter()
        {
            SkillRunRender.Instance.SendMessage(_entityData._entityId, RenderState.Enter, this);
        }

        public override void Render(RenderState renderState, IClipRenderEnv renderEnvEnv)
        {
            if (renderState == RenderState.Enter)
            {
                Transform bindTran = null;
                if (_bindType == EffectPointType.Avatar)
                {
                    bindTran = renderEnvEnv.GetBone(_bindName);
                }

                EffectRender.PlayEffect(bindTran, _asset as GameObject, _pos, _rotate, _scale,
                    (effect) => { _effectGo = effect; });
            }
            else if (renderState == RenderState.Exit)
            {
                if (_effectGo != null)
                {
                    EffectRender.StopEffect(_effectGo);
                }
            }
        }
    }
}